/**
 * 
 */
package org.iron.pages;

import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.iron.entities.Book;
import org.iron.services.NewBookService;
import org.iron.services.PrinterService;
import org.iron.services.WeatherService;
import org.tempuri.Device;

/**
 * @author iron
 * 
 */
public class Index
{
    @Property
    private String currentCity, pm25, temperature, wind, date, weather, pictureUrl, weather1, temperature1, wind1, date1, pictureUrl1, weather2, temperature2, wind2, date2, pictureUrl2, weather3,
            temperature3, wind3, date3, pictureUrl3;
    
    @Property
    private List<Device> devices;

    @Inject
    private WeatherService weatherService;
    
    @Inject
    private NewBookService newbookService;
    
    @Inject
    private PrinterService printerService;

    void onActivate()
    {

    }
    
    void setupRender()
    {
        updateWeather();
        updatePrinterStatus();

    }

    void onRefreshFromWeatherzone()
    {
        //更新天气预报
        updateWeather();

    }
    void onRefreshFromPrinterzone()
    {
        //更新天气预报
        updatePrinterStatus();

    }
    void updateWeather()
    {
        JSONObject json = weatherService.getWeather("广州");
        currentCity = ((JSONObject) json.getJSONArray("results").get(0)).getString("currentCity");
        pm25 = ((JSONObject) json.getJSONArray("results").get(0)).getString("pm25");
        temperature = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(0)).getString("temperature");
        wind = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(0)).getString("wind");
        date = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(0)).getString("date");
        weather = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(0)).getString("weather");
        pictureUrl = weatherService.getPictureUrl(weather);
        temperature1 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(1)).getString("temperature");
        wind1 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(1)).getString("wind");
        date1 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(1)).getString("date");
        weather1 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(1)).getString("weather");
        pictureUrl1 = weatherService.getPictureUrl(weather1);

        temperature2 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(2)).getString("temperature");
        wind2 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(2)).getString("wind");
        date2 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(2)).getString("date");
        weather2 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(2)).getString("weather");
        pictureUrl2 = weatherService.getPictureUrl(weather2);

        temperature3 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(3)).getString("temperature");
        wind3 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(3)).getString("wind");
        date3 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(3)).getString("date");
        weather3 = ((JSONObject) ((JSONObject) json.getJSONArray("results").get(0)).getJSONArray("weather_data").get(3)).getString("weather");
        pictureUrl3 = weatherService.getPictureUrl(weather1);
    }
    
    void updateNewBook()
    {
        List<Book> list = newbookService.getNewBooks();
    }

    void updatePrinterStatus()
    {
        devices = printerService.getDeviceList("");
    }
}